#Kiwi Editor is a framework for building WYSIWYG web editors.
#Copyright (C) 2011  Roosevelt de Lima Sardinha

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#Contact the author at roosevelt1@gmail.com (Roosevelt) 

class WorkspacesController < ApplicationController
  def create
   # Getting parameters
    json_str = params[:json]
    as_json = ActiveSupport::JSON
    
    json = as_json.decode(json_str)
    
    workspace = Workspace.new
    workspace.update_with_json(json)
        
    respond_to do |format|
      format.js  { render :json => workspace.id ? workspace.id : false}
    end 
  end
  
  def update
    # Getting parameters
    json_str = params[:json]
    as_json = ActiveSupport::JSON
    
    json = as_json.decode(json_str)
    
    workspace = Workspace.find(params[:id])
    workspace.update_with_json(json)
    
    respond_to do |format|
      format.js  { render :json => true}
    end
  end
  
  def destroy
    workspace = Workspace.find(params[:id])
    Workspace.destroy(workspace)
    
    respond_to do |format|
      format.js  { render :json => true}
    end
  end
  
  def index
    workspaces = Workspace.all
    all_workspaces_array = []
    workspaces_hash = {}
    
    workspaces.each do |workspace|
      all_workspaces_array << workspace.to_hash 
    end
    
    workspaces_hash["workspaces"] = all_workspaces_array
    
    respond_to do |format|
      format.js  { render :json => workspaces_hash}
      format.xml  { render :xml => workspaces_hash}
    end
  end
  
  def get
    workspace = Workspace.find(params[:id])   
    respond_to do |format|
      format.js  { render :json => workspace.to_hash}
      format.xml  { render :xml => workspace.to_hash}
    end
  end  
  
  def exists?
    workspace = Workspace.find_by_id(params[:id])    
    respond_to do |format|
      format.js  { render :json => workspace.nil? ? false : true}
    end
  end
end

# JSON format expected to a workspace
#{
#  current_profile: 5,  
#  profiles: [2,3,4],
#  instruction_boxes:[{
#    id: 'instruction_box',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/instruction_box.css
#    parent: 'main'
#  },{
#    id: 'instruction_box2,
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/instruction_box.css
#    parent: 'main'
#  }],
# property_boxes:[{
#    id: 'property_box',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/property_box.css
#    parent: 'main'    
#  },{
#    id: 'property_box2,
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/property_box.css
#    parent: 'main'    
#  }],
#  tool_boxes:[{
#    id: 'tool_box',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/tool_box.css
#    parent: 'main'
#  },{
#    id: 'tool_box2',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/tool_box.css
#    parent: 'main'
#  }],
#  edition_areas:[{
#    id: 'edition_area',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/edition_area.css
#    parent: 'main'
#  },{
#    id: 'edition_area2',
#    top: 20px,
#    left: 30px,
#    height: 300px,
#    width: 400px,
#    css_path: ./css/custom/edition_area.css
#    parent: 'main'
#  }]
#}



